﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

/*
 * Author:Conis
 * Date:
 * Description:Xml的叶子节点
 */

namespace Coins.Squirrel.Nodous.Xml
{
    public class XmlNodousLeaf : XmlNodousComponent, INodousLeaf
    {
        /// <summary>
        /// 创建叶子节点
        /// </summary>
        /// <param name="name">节点的名称</param>
        /// <param name="value">节点的值</param>
        protected internal XmlNodousLeaf(string name, string value) : base(name, value) { }
        /// <summary>
        /// 创建一个完全的叶子节点，需要保证值不包含Xml敏感字符，节点将以属性的方式保存
        /// </summary>
        /// <param name="name">节点的名称</param>
        /// <param name="value">节点的值</param>
        protected internal XmlNodousLeaf(string name, string value, bool isSafe)
            : base(name, value)
        {
            this.isSafe = isSafe;
        }

        /// <summary>
        /// 重载父类，创建具体节点
        /// </summary>
        protected override void CreateNode()
        {
            //安全数据直接添加到属性中
            if (this.isSafe)
            {
                XmlAttribute att = this.container.CreateAttribute(this.name);
                att.Value = this.value;
                this.parentElement.Attributes.Append(att);
            }
            else
            {
                //只有不安全的字符串，才需要用CData的方式
                XmlElement element = this.container.CreateElement(this.name);
                XmlCDataSection cdata = this.container.CreateCDataSection(this.value);
                element.AppendChild(cdata);
                this.parentElement.AppendChild(element);
                this.parentElement = element;
            }
        }
    }
}
